package reserve
import (
    //"time"
	"strings"
	"errors"
	"dhfshop/models"
	"github.com/astaxie/beego/orm"
)

//可预约时间段模板
type TimeslotStl struct {
	TimeslotSTLId int64   `orm:"column(timeslot_stl_id);pk;auto" json:"timeslot_stl_id" form:"timeslot_stl_id"`
	PartnerId     int64     `orm:"column(partner_id)" json:"partner_id" form:"partner_id"`
	SchemaId      int64   `orm:"column(schema_id)" json:"schema_id" form:"schema_id"`             //是否项目私有模板：公共模板0;私有模板:非0 
    PeriodSTLId   int64   `orm:"column(period_stl_id)" json:"period_stl_id" form:"period_stl_id"` //是否预约周期私有模板 
	TimeslotNo    int     `orm:"column(timeslot_no)" json:"timeslot_no" form:"timeslot_no"`       //时段序号
	Start         string  `orm:"column(start);null" json:"start" form:"start"`                    //时间段开始时间,比如: 11:00
	End           string  `orm:"column(end);null" json:"end" form:"end"`                          //时间段结束时间,比如: 12:00
	MaxCount      int     `orm:"column(max_count)" json:"max_count" form:"max_count"`             //最大可预约量
	Status        int     `orm:"column(status);default(1)" json:"status" form:"status"`           //是否有效: 1 有效; 0 无效
}

func init() {
	orm.RegisterModel(new(TimeslotStl))
}

func (u *TimeslotStl) TableUnique() [][]string {
	return [][]string{
		[]string{"PartnerId", "SchemaId", "PeriodSTLId", "TimeslotNo"},
	}
}


func InitDefaultTimeslotSTLs(partnerId int64) (err error) {
    timeSlotSTLRes := GetTimeslotSTLResource(partnerId)
	err = AddMultiTimeslotSTLs(timeSlotSTLRes)
	return
}

//系统时段公有模板
func GetTimeslotSTLResource(partnerId int64) (resources []*TimeslotStl) {
	resources = []*TimeslotStl{}
	//时段1
	resources = append(resources, &TimeslotStl{
		PartnerId:   partnerId,
		SchemaId:    0,
		PeriodSTLId: 0,
		TimeslotNo:  1,
		Start:       "09:00",
		End:         "10:00",
		MaxCount:    3,
        Status:      1,
	})
    //时段2
	resources = append(resources, &TimeslotStl{
		PartnerId:   partnerId,
		SchemaId:    0,
		PeriodSTLId: 0,
		TimeslotNo:  2,
		Start:       "10:00",
		End:         "11:00",
		MaxCount:    3,
		Status:      1,
	})
	//时段3
	resources = append(resources, &TimeslotStl{
		PartnerId:   partnerId,
		SchemaId:    0,
		PeriodSTLId: 0,
		TimeslotNo:  3,
		Start:       "11:00",
		End:         "12:00",
		MaxCount:    3,
		Status:      1,
	})
	//时段4
	resources = append(resources, &TimeslotStl{
		PartnerId:   partnerId,
		SchemaId:    0,
		PeriodSTLId: 0,
		TimeslotNo:  4,
		Start:       "13:00",
		End:         "14:00",
		MaxCount:    3,
		Status:      1,
	})
    //时段5
	resources = append(resources, &TimeslotStl{
		PartnerId:   partnerId,
		SchemaId:    0,
		PeriodSTLId: 0,
		TimeslotNo:  5,
		Start:       "14:00",
		End:         "15:00",
		MaxCount:    3,
		Status:      1,
	})
    //时段6
	resources = append(resources, &TimeslotStl{
		PartnerId:   partnerId,
		SchemaId:    0,
		PeriodSTLId: 0,
		TimeslotNo:  6,
		Start:       "15:00",
		End:         "16:00",
		MaxCount:    3,
		Status:      1,
	})
    //时段7
	resources = append(resources, &TimeslotStl{
		PartnerId:   partnerId,
		SchemaId:    0,
		PeriodSTLId: 0,
		TimeslotNo:  7,
		Start:       "16:00",
		End:         "17:00",
		MaxCount:    3,
		Status:      1,
	})
	return resources
}

//复制公有模板到项目私有模板
func CopyTimeslotSTL(partnerId,schemaId,periodSTLId int64,srcTimeslotStl []*TimeslotStl) (dstTimeslotStl []*TimeslotStl) {
	dstTimeslotStl = []*TimeslotStl{}
	for _, value := range srcTimeslotStl {
	    dstTimeslotStl = append(dstTimeslotStl, &TimeslotStl{
			PartnerId:   partnerId,
			SchemaId:    schemaId,
			PeriodSTLId: periodSTLId,
			TimeslotNo:  value.TimeslotNo,
			Start:       value.Start,
			End:         value.End,
			MaxCount:    value.MaxCount,
			Status:      value.Status,
		})
	}
	return dstTimeslotStl
}

func AddTimeslotSTL(m *TimeslotStl) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func AddMultiTimeslotSTLs(list []*TimeslotStl) (err error) {
	size := len(list)
	o := models.GetDefaultDB()
	if size > 1 {
	    _, err = o.InsertMulti(size, list) 
	}else{
	   _, err = o.Insert(list[0])
	}
	return
}

func GetTimeslotSTLById(id int64) (v *TimeslotStl, err error) {
	o := models.GetDefaultDB()
	v = &TimeslotStl{TimeslotSTLId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetAllTimeslotSTLs(query,order_by map[string]string) (lists []*TimeslotStl, err error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(TimeslotStl))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	if _, err = qs.All(&lists); err == nil {
		return
	}
	return
}
func UpdateTimeslotSTLById(m *TimeslotStl, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := TimeslotStl{TimeslotSTLId: m.TimeslotSTLId}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}
func DeleteTimeslotSTLById(id int64) (err error) {
	o := models.GetDefaultDB()
	v := TimeslotStl{TimeslotSTLId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}

func DeleteTimeslotSTLsByPartnerId(partnerId int64) (err error) {
    o := models.GetDefaultDB()
	_, err = o.QueryTable(new(TimeslotStl)).Filter("partner_id", partnerId).Delete()
	return
}
